package com.zhoug.zmui3.chart.marker;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.PointF;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;

import com.zhoug.common3.utils.LogUtils;
import com.zhoug.zmui3.ZMUIShape;
import com.zhoug.zmui3.chart.ChartEntry;
import com.zhoug.zmui3.chart.IMarker;
import com.zhoug.zmui3.chart.ZMUIChart;

import androidx.annotation.LayoutRes;

/**
 * @Author 35574
 * @Date 2021/2/22
 * @Description
 */
public abstract class MarkerView extends RelativeLayout implements IMarker {
    private static final String TAG = ">>>MarkerView";
    private static final boolean DEBUG = false;
    private boolean integerValue;//数据是否是整型
    private ZMUIChart chart;
    private int xiaoshu = 1;//小数位数
    private int autoCancelMarkerSecond = 3;//单位秒,大于0表示时间到了自动关闭marker
    /**
     * {@link ZMUIShape#CIRCLE}
     * {@link ZMUIShape#RECT}
     * {@link ZMUIShape#LINE}
     * {@link ZMUIShape#LINE}
     */
    protected int shape=ZMUIShape.RECT;


    public MarkerView(Context context, ZMUIChart chart, @LayoutRes int layoutId) {
        super(context);
        View view = LayoutInflater.from(context).inflate(layoutId, this, false);
        addView(view);
        this.chart = chart;
    }

    @Override
    public void refreshContent(ChartEntry value, int index) {
        measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
                MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
        layout(0, 0, getMeasuredWidth(), getMeasuredHeight());
    }

    @Override
    public void draw(Canvas canvas, float posX, float posY) {
//        super.draw(canvas);
        int saveId = canvas.save();
        if (DEBUG) {
            LogUtils.d(TAG, "draw:posX=" + posX + ",posY=" + posY);
        }
        PointF offsetForDrawingAtPoint = getOffsetForDrawingAtPoint(posX, posY);
        canvas.translate(offsetForDrawingAtPoint.x, offsetForDrawingAtPoint.y);
        draw(canvas);
        canvas.restoreToCount(saveId);

        if (autoCancelMarkerSecond > 0) {
            if (chart != null) {
                //移除上一个任务
                chart.removeCallbacks(autoCancelMarkerTask);
                chart.postDelayed(autoCancelMarkerTask, autoCancelMarkerSecond * 1000);
            }
        }

    }

    private PointF mOffset = new PointF();

    @Override
    public PointF getOffsetForDrawingAtPoint(float posX, float posY) {
        int width = getWidth();
        int height = getHeight();
        if (DEBUG) {
            LogUtils.d(TAG, "getOffsetForDrawingAtPoint:width="+width+",height="+height);
        }
        ZMUIChart chart = getChart();
        int chartW = chart.getWidth();
        int chartH = chart.getHeight();
        if (DEBUG) {
            LogUtils.d(TAG, "getOffsetForDrawingAtPoint:chartW="+chartW+",chartH="+chartH);
        }
        if (width  <= chartW - posX) {
            mOffset.x = posX;
        } else {
            mOffset.x = chartW - width;
        }
        if (height  < chartH - posY) {
            mOffset.y = posY;
        } else {
            mOffset.y = chartH - height;
        }
        if (mOffset.x < 0) {
            mOffset.x = 0;
        }
        if (mOffset.y < 0) {
            mOffset.y = 0;
        }
        if (DEBUG) {
            LogUtils.d(TAG, "getOffsetForDrawingAtPoint:mOffset="+mOffset);
        }
        return mOffset;
    }

    private Runnable autoCancelMarkerTask = new Runnable() {
        @Override
        public void run() {
            //高亮
            if (chart != null && chart.isValuesToHighlight()) {
                chart.setHighlightIndex(-1);
                chart.invalidate();
                if (DEBUG) {
                    LogUtils.d(TAG, "refreshContent:关闭marker");
                }
            }
        }
    };

    public boolean isIntegerValue() {
        return integerValue;
    }

    public void setIntegerValue(boolean integerValue) {
        this.integerValue = integerValue;
    }

    public ZMUIChart getChart() {
        return chart;
    }

    public void setXiaoshu(int xiaoshu) {
        this.xiaoshu = xiaoshu;
    }

    protected int getXiaoshu() {
        return xiaoshu;
    }

    public void setAutoCancelMarkerSecond(int autoCancelMarkerSecond) {
        this.autoCancelMarkerSecond = autoCancelMarkerSecond;
    }

    public void setShape(int shape) {
        this.shape = shape;
    }

}
